<?php
/**
 * 冰川时代5海报任务后台管理
 */
class Action
{
    private $per_page = 20;

    /**
     * 海报列表
     * @return Response
     */
    public function index() {
        $page_no = $status = $search_filter = null;

        //给变量初始化一个值,如果获取到了其get值则重新赋值
        extract(URLTracker::init(array(
            'page_no' => 1,
            'status' => -1,
            'search_filter' => array()
        )));

        $where = '';
        if (isset($search_filter['realname']) && $search_filter['realname']) {
            $where .= ' and u.realname like "%' . $search_filter['realname'] . '%"';
        }
        if (isset($search_filter['title']) && $search_filter['title']) {
            $where .= ' and s.title like "%' . $search_filter['title'] . '%"';
        }
        if (isset($search_filter['province_id']) && $search_filter['province_id']) {
            $where .= ' and s.province_id =' . $search_filter['province_id'];
        }
        if (isset($search_filter['city_id']) && $search_filter['city_id']) {
            $where .= ' and s.city_id =' . $search_filter['city_id'];
        }
        if (isset($search_filter['district_id']) && $search_filter['district_id']) {
            $where .= ' and s.district_id =' . $search_filter['district_id'];
        }
        if ($status != -1) {
            $where .= ' and t.status = ' . $status;
        }

        if (!ONDEV) {
            $where .= ' and u.is_enable = 1';
            $where .= ' and s.is_enable = 1';
        }

        $count_sql = 'select count(t.id) as num from topic_glacial_era t left join `user` u on t.parent_id = u.id  left join store s on t.store_id = s.id where u.status = 1 and s.status != 0 and s.is_delete = 1 ' . $where;
        $photo_count = _model('topic_glacial_era')->getAll($count_sql)[0]['num'];

        //分页配置
        $pager = new Pager($this->per_page);
        if ($pager->generate($photo_count)) {
            Response::assign('pager', $pager);
        }

        $sql = 'select t.*, u.realname, s.cover, s.title, s.title_branch from topic_glacial_era t left join `user` u on t.parent_id = u.id  left join store s on t.store_id = s.id where u.status = 1 and s.status != 0 and s.is_delete = 1 ' . $where . ' order by t.id desc ' . $pager->getLimit($page_no);
        $photo_list = _model('topic_glacial_era')->getAll($sql);

        foreach ($photo_list as $key => $value) {
            $photo_list[$key]['src'] = $this->format_image($value['src']);
            $photo_list[$key]['cover'] = $this->format_image($value['cover']);
        }

        Response::assign('status', $status);
        Response::assign('photo_list', $photo_list);
        Response::assign('search_filter', $search_filter);
        Response::display('admin/list.html');
    }

    /**
     * 修改状态
     * @return json
     */
    public function change_status() {
        $id = an_request('id', 0);
        $status = an_request('status', 0);

        //数据必填及其他校验
        $this->change_status_validation($id, $status);

        _model('topic_glacial_era')->update($id, array('status' => $status));
        return_data(0, 'success');
    }

    /**
     * 导出海报任务
     * @return file
     */
    public function export() {
        $export_start_date = an_request('export_start_date', '');
        $export_end_date = an_request('export_end_date', '');

        $where = '1 = 1 ';
        if ($export_start_date) {
            $where .= 'and t. upload_time >= "' . $export_start_date . '"';
        }
        if ($export_end_date) {
            $where .= 'and t. upload_time <= "' . $export_end_date . '"';
        }

        $count_sql = 'select count(t.id) as num from topic_glacial_era t where '. $where;
        $count = _model('topic_glacial_era')->getALL($count_sql)[0]['num'];
        if ($count > 2000) {
            header('content-type:text/html;charset=utf-8');
            exit('导出数据过多，请按时间段导出');
        }

        $row[] = '海报任务ID, 合伙人, 商铺ID, 商铺名称, 上传时间, 状态';

        if (!ONDEV) {
            $where .= ' and u.is_enable = 1';
            $where .= ' and s.is_enable = 1';
        }
        $sql = 'select t.*, u.realname, s.id as store_id, s.title, s.title_branch from topic_glacial_era t left join `user` u on t.parent_id = u.id  left join store s on t.store_id = s.id where ' . $where . ' and u.status = 1 and s.status != 0 and s.is_delete = 1';
        $photo_list = _model('topic_glacial_era')->getALL($sql);
        if ($photo_list) {
            foreach ($photo_list as $value) {
                $row[] = join(', ', array(
                    $value['id'],
                    $value['realname'],
                    $value['store_id'],
                    $value['title'] . '-' . $value['title_branch'],
                    $value['upload_time'],
                    glacial_era_config::$status[$value['status']]
                ));
            }
        }
        echo iconv('UTF-8', 'GBK//IGNORE', join("\n", $row));
        $this->export_csv('冰川时代5海报任务列表'. time() . '.csv', 0);
    }

    /**
     * 图片打包下载
     * @return file
     */
    public function download() {
        $topic_name = an_request('topic_name', '');
        if (empty($topic_name) || !in_array($topic_name, topic_config::$TOPIC)) {
            return false;
        }

        return _widget('topic.data')->download($topic_name);
    }

    private function export_csv($filename, $data) {
        $file_hwnd = fopen('' . $filename, "w");
        header("Content-type:text/csv");
        header("Content-Disposition:attachment;filename=" . $filename);
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
        header('Expires:0');
        header('Pragma:public');
        fwrite($file_hwnd, $data);
        fclose($file_hwnd);
    }

    /**
     * 格式化图片路径
     * @param string $src 图片路径
     * @return string
     */
    private function format_image($src) {
        if ($src) {
            return _image($src);
        } else {
            return store_helper::cover($src);
        }
    }

    /**
     * 改变状态必填及其他校验
     * @param int $id 海报任务id
     * @param int $staus 要更改的状态
     * @return json
     */
    private function change_status_validation($id, $status) {
        if (!$id) {
            return_data(1, '请选择要更改的记录');
        }
        if (!$status) {
            return_data(1, '请选择要更改的状态');
        }
        if (!in_array($status, array(1, 2))) {
            return_data(1, '状态不合法');
        }
        $res = _model('topic_glacial_era')->field('id', 'status')->read($id);
        if (!$res) {
            return_data(1, '该条记录不存在');
        }
        if ($status == $res['status']) {
            return_data(1, '您要修改的状态和当前状态一致');
        }
    }
}